﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YouHP.Model;
using YouHP.Atom;

namespace YouHP.Logic
{
    public class TreatmentHistoryLogic
    {
        public int AddTreatmentHistory(TreatmentHistoryInfo info)
        {
            //先检查是否会产生时间上的冲突
            PatientTreatmentHistories treatmentHis = new PatientTreatmentHistories();
            if (0 !=treatmentHis.AddRecord(info))
            {
                return -1 ;
            }

             TreatmentHistoryInfo curInfo = treatmentHis.GetCurrentTreatment(info.PRId, info.TreatmentId);
           
            if ( (curInfo.Id <=0 || info.StartDate > curInfo.StartDate ) && treatmentHis.UpdateCurrentTreatment(info) != 0)
            {
                return -2;
            }

            //更新
             return 0; 
        }

        public List<TreatmentHistoryInfo> GetHistories(int prId, int treatmentId, DateTime startDate, DateTime endDate)
        {
            PatientTreatmentHistories treatmentHis = new PatientTreatmentHistories();
            int iCount ;
            return treatmentHis.GetHistoriesByTreatmentId(prId, treatmentId, startDate, endDate, 10000, 0, out iCount);

        }
        public int UpdateTreatmentHistory(TreatmentHistoryInfo info)
        {
            PatientTreatmentHistories treatmentHis = new PatientTreatmentHistories();

            int iRet = treatmentHis.UpdateRecord(info);
            if (0 != iRet)
            {
                return -3;
            }
            TreatmentHistoryInfo curInfo = treatmentHis.GetCurrentTreatment(info.PRId, info.TreatmentId);

            if (info.StartDate > curInfo.StartDate && treatmentHis.UpdateCurrentTreatment(info) != 0)
            {
                return -2;
            }

            return 0;

        }

        public int DeleteTreatmentHistory(TreatmentHistoryInfo info)
        {
            PatientTreatmentHistories treatmentHis = new PatientTreatmentHistories();
            return treatmentHis.DeleteRecord(info);
        }

        public List<TreatmentHistoryInfo> GetHistories(int iPrId, int iPageSize, int iPageNo, out int iCount)
        {
            PatientTreatmentHistories treatmentHis = new PatientTreatmentHistories();
            return treatmentHis.GetHistoriesByPrId(iPrId, iPageSize, iPageNo, out iCount);
        }

        public List<TreatmentHistoryInfo> GetHistories(int iPrId, int iTreatmentId, int iPageSize, int iPageNo, out int iCount)
        {
            PatientTreatmentHistories treatmentHis = new PatientTreatmentHistories();
            if (iTreatmentId == 0 )
            {
                return treatmentHis.GetHistoriesByPrId(iPrId, iPageSize, iPageNo, out iCount);
            }
            else
            {
                return treatmentHis.GetHistoriesByTreatmentId(iPrId, iTreatmentId, iPageSize, iPageNo, out iCount);
            }
        }

        public TreatmentHistoryInfo GetCurrentTreatment(int iPrId, int iTreatmentId)
        {
            PatientTreatmentHistories treatmentHis = new PatientTreatmentHistories();
            return treatmentHis.GetCurrentTreatment(iPrId, iTreatmentId);
        }

        public List<TreatmentHistoryInfo> GetTreatmentsByPrId(int iPrId)
        {
            PatientTreatmentHistories treatmentHis = new PatientTreatmentHistories();
            return treatmentHis.GetTreatmentsByPrId(iPrId);
        }

        public TreatmentHistoryInfo GetHistory(int HistoryId)
        {
            PatientTreatmentHistories treatmentHis = new PatientTreatmentHistories();
            return treatmentHis.GetHistoryById(HistoryId);
        }

        public int DeleteTreatmentHistories(int PrId, int TreatmentId)
        {
            PatientTreatmentHistories treatmentHis = new PatientTreatmentHistories();
            return treatmentHis.DeleteRecord(PrId, TreatmentId); 
        }

    }
}
